/*
 *全局参数业务逻辑层
 */
package com.uyayo.portal.biz;

import com.uyayo.framework.common.util.date.DateFormatExt;
import com.uyayo.system.config.service.SysParamService;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Date;

@Component
public class SmsEmailBiz {

	@Autowired
	private SysParamService paramsService;
	@Autowired
	private JavaMailSender mailSender; //自动注入的Bean

	@Value("${spring.mail.username}")
	private String Sender; //读取配置文件中的参数

	/**
	 * 发送邮件
	 * @return
	 */
	public String sendEmail(String username,String code,String email,Date date){

		MimeMessage message;
		message = mailSender.createMimeMessage();
		try {
			MimeMessageHelper helper = new MimeMessageHelper(message, true);
			helper.setFrom(Sender);
			helper.setTo(email); //自己给自己发送邮件
			helper.setSubject("【j2eecms】 验证码");
			helper.setText("<div id=\"qm_con_body\"><div id=\"mailContentContainer\" >" +
					"<div style=\"width: 100%; background-color: #F7F7F7;\">" +
					"<div style=\"height: 1px;\"></div>" +
					"<div style=\"background: #fff; border: 1px solid #ccc; margin:2%; padding:0 30px\">" +
					"<div style=\"height: 40px;\"></div>" +
					"<div style=\"font-weight: bolder; height: 30px; line-height: 30px;\">亲爱的用户：</div>" +
					"<div style=\"height: 30px;\"></div>" +
					"<div style=\"height: 30px; line-height: 30px;\">您的验证码为：</div>" +
					"<span style=\"border-bottom: 1px dashed rgb(204, 204, 204); color: #F90; font-size: 18px; font-weight: bold; height: 30px; line-height: 30px;\"><span style=\"border-bottom: 1px dashed rgb(204, 204, 204); z-index: 1; position: static;\" t=\"7\" onclick=\"return false;\" data=\""+code+"\">"+code+"</span></span>" +
					"<div style=\"height: 40px;\"></div>" +
					"<div>跳转链接：<a href=\"http://localhost:8000/register/verify/"+username+"/"+code+"\" target=\"_blank\">http://localhost:8000/register/verify/"+username+"/"+code+"</a></div>" +
					"<div style=\"height: 40px;\"></div>" +
					"<div>J2EECMS团队</div>" +
					"<div style=\"height: 30px;\"><span style=\"border-bottom: 1px dashed rgb(204, 204, 204); position: relative;\" t=\"5\" times=\"\">"+DateFormatUtils.format(date, DateFormatExt.DF_YYYY_MM_DD)+"</span></div>" +
					"<div style=\"height: 40px;\"></div>" +
					"</div>" +
					"<div style=\"height: 1px;\"></div>" +
					"</div>" +
					"<style type=\"text/css\">.qmbox style, .qmbox script, .qmbox head, .qmbox link, .qmbox meta {display: none !important;}</style></div></div>",true);
		} catch (MessagingException e) {
			e.printStackTrace();
		}

		mailSender.send(message);
		return null;
	}

}
